package cn.yto.print.config;

import cn.yto.print.utils.CommonUtil;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;

import java.util.concurrent.*;

public class ThreadPoolManager {

    private static final ExecutorService executorService = new ThreadPoolExecutor(5, 20, 60L, TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(100), new CustomizableThreadFactory("assistant-worker-"), new ThreadPoolExecutor.CallerRunsPolicy());

    public static ExecutorService getExecutorService() {
        return executorService;
    }

    public static ScheduledExecutorService getSingleScheduledExecutor() {
        return Executors.newSingleThreadScheduledExecutor(new CustomizableThreadFactory("assistant-scheduled-worker-"));
    }

    public static ScheduledExecutorService getSingleDaemonExecutor() {
        return Executors.newSingleThreadScheduledExecutor(r -> {
            Thread thread = new Thread(r);
            thread.setDaemon(true);
            thread.setName("assistant-daemon-worker-");
            return thread;
        });
    }

    public static void destroy() {
        CommonUtil.gracefulShutdown(executorService);
    }

}
